import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录', hidden: true }
  },
  {
    path: '/',
    component: () => import('../layout/index.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'HomeFilled' }
      }
    ]
  },
  {
    path: '/knowledge',
    component: () => import('../layout/index.vue'),
    redirect: '/knowledge/herbs',
    meta: { title: '知识管理', icon: 'Collection' },
    children: [
      {
        path: 'herbs',
        name: 'HerbsKnowledge',
        component: () => import('../views/knowledge/herbs/index.vue'),
        meta: { title: '中药知识管理' }
      },
      {
        path: 'recipes',
        name: 'RecipesKnowledge',
        component: () => import('../views/knowledge/recipes/index.vue'),
        meta: { title: '药膳知识管理' }
      },
      {
        path: 'wellness',
        name: 'WellnessKnowledge',
        component: () => import('../views/knowledge/wellness/index.vue'),
        meta: { title: '养生知识管理' }
      }
    ]
  },
  {
    path: '/shop',
    component: () => import('../layout/index.vue'),
    redirect: '/shop/products',
    meta: { title: '商城管理', icon: 'ShoppingCart' },
    children: [
      {
        path: 'products',
        name: 'Products',
        component: () => import('../views/shop/products/index.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'products/:id',
        name: 'ProductDetail',
        component: () => import('../views/shop/products/detail/index.vue'),
        meta: { title: '商品详情', hidden: true }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('../views/shop/orders/index.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'orders/:id',
        name: 'OrderDetail',
        component: () => import('../views/shop/orders/detail/index.vue'),
        meta: { title: '订单详情', hidden: true }
      }
    ]
  },
  {
    path: '/forum',
    component: () => import('../layout/index.vue'),
    redirect: '/forum/posts',
    meta: { title: '论坛管理', icon: 'ChatLineRound' },
    children: [
      {
        path: 'posts',
        name: 'Posts',
        component: () => import('../views/forum/posts/index.vue'),
        meta: { title: '帖子管理' }
      },
      {
        path: 'comments',
        name: 'Comments',
        component: () => import('../views/forum/comments/index.vue'),
        meta: { title: '评论管理' }
      }
    ]
  },
  {
    path: '/user',
    component: () => import('../layout/index.vue'),
    redirect: '/user/list',
    meta: { title: '用户管理', icon: 'User' },
    children: [
      {
        path: 'list',
        name: 'UserList',
        component: () => import('../views/users/list/index.vue'),
        meta: { title: '用户列表' }
      },
      {
        path: 'admin',
        name: 'AdminList',
        component: () => import('../views/user/admin/index.vue'),
        meta: { title: '管理员管理' }
      }
    ]
  },
  {
    path: '/404',
    component: () => import('../views/error/404.vue'),
    meta: { hidden: true }
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404',
    meta: { hidden: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 中药养生平台管理系统` : '中药养生平台管理系统'
  
  // 登录验证
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 